package com.dc.business.datacollectbusiness.controller;

import com.dc.business.datacollectbusiness.entity.CompanyUserRelationShip;
import com.dc.business.datacollectbusiness.entity.Meter;
import com.dc.business.datacollectbusiness.entity.User;
import com.dc.business.datacollectbusiness.mapper.CompanyMapper;
import com.dc.business.datacollectbusiness.model.*;
import com.dc.business.datacollectbusiness.model.大屏显示.饼图.Btumodel;
import com.dc.business.datacollectbusiness.model.大屏显示.采集器报警.Collectormodel;
import com.dc.business.datacollectbusiness.model.大屏显示.柱状图.Zztmodel;
import com.dc.business.datacollectbusiness.model.大屏显示.折线图.ZxtmodelDays;
import com.dc.business.datacollectbusiness.model.大屏显示.折线图.ZxtmodelHours;
import com.dc.business.datacollectbusiness.model.大屏显示.折线图.ZxtmodelMonth;
import com.dc.business.datacollectbusiness.model.表格数据.*;
import com.dc.business.datacollectbusiness.service.*;
import com.dc.business.datacollectbusiness.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/api/meter")
public class MeterController {

    @Autowired
    private MeterService meterService;

    @Autowired
    private UserService userService;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private CompanyUserRelationShipService companyUserRelationShipService;

    @Autowired
    private CollectorPassageService collectorPassageService;

    @Autowired
    private CollectorService collectorService;

    @PostMapping("/insert")
    public Result insertMeter(@RequestBody Meter meter) {

        int b=0;
        b=meterService.insertMeter(meter);
        if(b==1){
            return Result.success(b);
        }else if (b==2){
            return Result.error("插入失败");
        }else {
            return Result.error("表号重复");
        }
    }

    @DeleteMapping("/delete")
    public Result deleteMeter(@RequestParam int meterId) {
        boolean b=false;
        b=meterService.deleteMeterById(meterId);
        if(b!=false){
            return Result.success(b);
        }else {
            return Result.error("删除失败");
        }
    }

    @PostMapping("/update")
    public Result updateMeter(@RequestBody Meter meter) {
        boolean b=false;
        b=meterService.updateMeter(meter);
        if(b!=false){
            return Result.success(b);
        }else {
            return Result.error("修改失败");
        }
    }

    @PostMapping("/seletebycengji")
    public Result getMeterBycengji(@RequestBody Meter meter) {
        Map<String,Object> map=new HashMap<>();
        map= ThreadLocalUtil.get();
        User userupdate=userService.findUserByUsername(map.get("username").toString());
        if(meter.getCompanyId()!=null){
            System.out.println("点击");
            List<Meter> meterList=meterService.seleteMeterBycengji(meter);
            if (meterList.isEmpty()){
                return Result.error(meterList);
            }else {
                return Result.success(meterList);
            }
        }else {
            if (userupdate.getUserType().equals("超级管理员")){
                List<Meter> meterList=meterService.seleteMeterBycengji(meter);
                if (meterList.isEmpty()){
                    return Result.error(meterList);
                }else {
                    return Result.success(meterList);
                }
            }else if(userupdate.getUserType().equals("普通用户")){
                int companyId= companyMapper.getCompanyIdByName(userupdate.getCompanyName());
                meter.setCompanyId(companyId);
                List<Meter> meterList=meterService.seleteMeterBycengji2(meter);
                if (meterList.isEmpty()){
                    return Result.error(meterList);
                }else {
                    return Result.success(meterList);
                }
            }else {
                List<Meter> meterList=new ArrayList<>();

                List<CompanyUserRelationShip> companyUserRelationShipList= companyUserRelationShipService.getAllCompanyIdByUserid(userupdate.getUserId());
                for (CompanyUserRelationShip companyUserRelationShip:companyUserRelationShipList){
                    meter.setCompanyId(companyUserRelationShip.getCompanyId());
                    List<Meter> meterList2=meterService.seleteMeterBycengji2(meter);
                    meterList.addAll(meterList2);
                }

                return Result.success(meterList);
            }
        }




    }

    @PostMapping("/deleteall")
    public Result deleteMeterall(@RequestBody Meterarr meterarr) {
        boolean b=false;
        b=meterService.deleteMeterByIdall(meterarr);
        if(b!=false){
            return Result.success(b);
        }else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("/seletebyid")
    public Result selectMeterByid(@RequestParam int meterId) {
        Meter meter=meterService.selectMeterById(meterId);
        if(meter==null){
            return Result.error("company not found");
        }else {
            return Result.success(meter);
        }
    }

    @GetMapping("/setmeter")
    public Result setmeter(@RequestParam int meterId,@RequestParam int number,@RequestParam int collectorPassageId,@RequestParam String scheduleNumber) {
        boolean b=false;
        System.out.println(11111);
        //配置点位
        try {
            b=meterService.setMeter(meterId,number,collectorPassageId);
            if(b!=false){
                //修改采集器通道状态和添加表编号

                b=collectorPassageService.updateCollectorPassagestate(collectorPassageId,scheduleNumber);

            }else {
                return Result.error("配置失败");
            }
        }catch (Exception e){

        }finally {
            if(b!=false){
                return Result.success(b);
            }else {
                return Result.error("配置失败");
            }
        }
    }

    @GetMapping("/getsetmeter")
    public Result getsetmeter(@RequestParam int meterId) {
        Meter meter=meterService.getsetMeter(meterId);
        if(meter==null){
            return Result.error("company not found");
        }else {
            return Result.success(meter);
        }
    }


    @PostMapping("/getmeterdate")
    public Result getmeterdate(@RequestBody Meter meter) {
        System.out.println(meter.getCountType());
        List<Meterdate> meterdateList=new ArrayList<>();
        //小时
        List<MeterdateHourLj> meterdateListLj=new ArrayList<>();
        List<MeterdateHourXh> meterdateListXh=new ArrayList<>();
        //天
        List<MeterdateDayLj> meterdateDayLjs=new ArrayList<>();
        List<MeterdateDayXh> meterdateDayXhs=new ArrayList<>();
        //月
        List<MeterdateMounthLj> meterdateMounthLjs=new ArrayList<>();
        List<MeterdateMounthXh> meterdateMounthXhs=new ArrayList<>();

        Map<String,Object> map=new HashMap<>();
        map= ThreadLocalUtil.get();

        //meter.getMeterName()为全部时,和空一样,查全部
        if(meter.getMeterName()!=null){
            if(meter.getMeterName().equals("全部")){
            System.out.println("全部");
            meter.setMeterName(null);
        }
        }


        User userupdate=userService.findUserByUsername(map.get("username").toString());
        if(meter.getCompanyId()!=null){
            //点击
            if(meter.getCountType().equals("小时")){
                if(meter.getDataTypeA().equals("累计值")){
                    meterdateListLj =meterService.getMeterDateByHourLj(meter);
                }else {
                    meterdateListXh =meterService.getMeterDateByHourXh(meter);
                }
            }
            else if(meter.getCountType().equals("天")){
                if(meter.getDataTypeA().equals("累计值")){
                    meterdateDayLjs =meterService.getMeterDateByDayLj(meter);
                }else {
                    meterdateDayXhs =meterService.getMeterDateByDayXh(meter);
                }

            }
            else if(meter.getCountType().equals("月")){
                if(meter.getDataTypeA().equals("累计值")){
                    meterdateMounthLjs =meterService.getMeterDateByMounthLj(meter);
                }else {
                    meterdateMounthXhs =meterService.getMeterDateByMounthXh(meter);
                }
            }
        }else {
            //第一次渲染页面
            if (userupdate.getUserType().equals("超级管理员")){
                if(meter.getCountType().equals("小时")){
                    if(meter.getDataTypeA().equals("累计值")){
                        meterdateListLj =meterService.getMeterDateByHourLj(meter);
                    }else {
                        meterdateListXh =meterService.getMeterDateByHourXh(meter);
                    }

                }
                else if(meter.getCountType().equals("天")){
                    if(meter.getDataTypeA().equals("累计值")){
                        meterdateDayLjs =meterService.getMeterDateByDayLj(meter);
                    }else {
                        meterdateDayXhs =meterService.getMeterDateByDayXh(meter);
                    }
                }
                else if(meter.getCountType().equals("月")){
                    if(meter.getDataTypeA().equals("累计值")){
                        meterdateMounthLjs =meterService.getMeterDateByMounthLj(meter);
                    }else {
                        meterdateMounthXhs =meterService.getMeterDateByMounthXh(meter);
                    }
                }

            }else{
                //普通用户或者维护人员
                if(meter.getCountType().equals("小时")){
                    List<CompanyUserRelationShip> companyUserRelationShipList= companyUserRelationShipService.getAllCompanyIdByUserid(userupdate.getUserId());
                    int sum=0;
                    if(meter.getDataTypeA().equals("累计值")){

                        meter.setCompanyId(companyUserRelationShipList.get(0).getCompanyId());
                        meterdateListLj=meterService.getMeterDateByHourLj2(meter);
                    }else {

                            meter.setCompanyId(companyUserRelationShipList.get(0).getCompanyId());
                            meterdateListXh=meterService.getMeterDateByHourXh2(meter);
                    }

                }
                else if(meter.getCountType().equals("天")){
                    List<CompanyUserRelationShip> companyUserRelationShipList= companyUserRelationShipService.getAllCompanyIdByUserid(userupdate.getUserId());
                    int sum=0;
                    if(meter.getDataTypeA().equals("累计值")){
                        meter.setCompanyId(companyUserRelationShipList.get(0).getCompanyId());
                        meterdateDayLjs=meterService.getMeterDateByDayLj2(meter);
                        System.out.println(meterdateDayLjs);
                    }else {
                        meter.setCompanyId(companyUserRelationShipList.get(0).getCompanyId());
                        meterdateDayXhs=meterService.getMeterDateByDayXh2(meter);
                    }

                }
                else if(meter.getCountType().equals("月")){
                    List<CompanyUserRelationShip> companyUserRelationShipList= companyUserRelationShipService.getAllCompanyIdByUserid(userupdate.getUserId());
                    int sum=0;
                    if(meter.getDataTypeA().equals("累计值")){
                        meter.setCompanyId(companyUserRelationShipList.get(0).getCompanyId());
                        meterdateMounthLjs=meterService.getMeterDateByMounthLj2(meter);
                        System.out.println(meterdateDayLjs);
                    }else {
                        meter.setCompanyId(companyUserRelationShipList.get(0).getCompanyId());
                        meterdateMounthXhs=meterService.getMeterDateByMounthXh2(meter);
                    }

                }


            }
        }

        if(meter.getDataTypeA().equals("累计值")){
            if (meter.getCountType().equals("小时")){
                if(meterdateListLj.isEmpty()){
                    return Result.successfy(meterdateListLj,0);
                }else {
                    return Result.successfy(meterdateListLj,meterdateListLj.get(0).getSum());
                }
            } else if (meter.getCountType().equals("天")) {
                if(meterdateDayLjs.isEmpty()){
                    return Result.successfy(meterdateDayLjs,0);
                }else {
                    return Result.successfy(meterdateDayLjs,meterdateDayLjs.get(0).getSum());
                }
            }else {
                if(meterdateMounthLjs.isEmpty()){
                    return Result.successfy(meterdateMounthLjs,0);
                }else {
                    return Result.successfy(meterdateMounthLjs,meterdateMounthLjs.get(0).getSum());
                }
            }

        }else {
            if (meter.getCountType().equals("小时")){
                if(meterdateListXh.isEmpty()){
                    return Result.successfy(meterdateListXh,0);
                }else {
                    return Result.successfy(meterdateListXh,meterdateListXh.get(0).getSum());
                }
            } else if (meter.getCountType().equals("天")) {
                if(meterdateDayXhs.isEmpty()){
                    return Result.successfy(meterdateDayXhs,0);
                }else {
                    return Result.successfy(meterdateDayXhs,meterdateDayXhs.get(0).getSum());
                }
            }else {
                if(meterdateMounthXhs.isEmpty()){
                    return Result.successfy(meterdateMounthXhs,0);
                }else {
                    return Result.successfy(meterdateMounthXhs,meterdateMounthXhs.get(0).getSum());
                }
            }

        }
    }

    //折线图
    @PostMapping("/getmeterdateDpZxt")
    public Result getmeterdateDpZxt(@RequestBody Meter meter) {
        if(meter.getStartTime()==null){
            meter.setStartTime("昨天");
        }

        System.out.println(meter.getStartTime());
        if(Objects.equals(meter.getStartTime(), "今天")){
            ZxtmodelHours zxtmodelHours=meterService.getmeterdateDpZxJt(meter);
            return Result.success(zxtmodelHours);
        }else if(Objects.equals(meter.getStartTime(), "昨天")){
            ZxtmodelHours zxtmodelHours=meterService.getmeterdateDpZxZt(meter);
            return Result.success(zxtmodelHours);
        }else if(Objects.equals(meter.getStartTime(), "近一周")){
            ZxtmodelDays zxtmodelDays=meterService.getmeterdateDpZxJqt(meter);
            return Result.success(zxtmodelDays);
        }else {
            ZxtmodelMonth zxtmodelMonth=meterService.getmeterdateDpZxYgy(meter);
            return Result.success(zxtmodelMonth);
        }

    }


    //柱状图
    @PostMapping("/getmeterdateDpZx")
    public Result getmeterdateDpZx(@RequestBody Meter meter) {
        List<Zztmodel> list=meterService.getmeterdateDpZx(meter);

        return Result.success(list);
    }

    //饼图
    @PostMapping("/getmeterdateDpBt")
    public Result getmeterdateDpBt(@RequestBody Meter meter) {
        List<Btumodel> list=meterService.getmeterdateDpBt(meter);

        return Result.success(list);
    }

    //采集器状态信息
    @GetMapping("/getCollectorZt")
    public Result getCollectorBj() {
        List<Collectormodel> collectormodelList= meterService.getAllState();
        int sum=0;
        return Result.successfy(collectormodelList,sum);
    }



}
